# vim: ts=4:sw=4:syntax=perl

# Take all the args and update portal ... and then
# redirect to page in the LP parameter.
use gri_secmgr qw(get_user_name redirect_header);
use gri_user_alert_config;
use File::Basename;
use xml_helper;
use Data::Dumper;
use gri2_requests;

# Parameters passed from the form
# id (hidden)
# comments (text)
# option_b (radio, values comment, approve, reject

sub mm_outstanding_approval_request_view_process {
my $_aa=new xml_helper();

	my $id=$::PARAM{id};
	my $comments=$::PARAM{comments};
	my $option_b=$::PARAM{option_b};
	my $user_name=get_user_name();
	if(! $user_name) {
		html_error("Not logged in - aborting.",1);
	}

	# Firstly define the comment we need to add to the comment field
	# depending on whether owner or voter for the portal in question.

	my $req_dir=$::GRI_FRONTEND->get_directory("requests");
	my $rdb=new gri2_requests("$req_dir/requests.db");
	if(!defined($rdb)) {
		my ($rc,$str);
		($rc,$str)=gri2_requests::error();
		my $error="Failed to open requests database.";
		my $serror=$str;
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error).",previouspage=";
		redirect_header(path => $link);
		exit;
	}

	my $request=$rdb->get_request($id);
	if(!defined($request)) {
		my $error="Request '$id' is not defined.";
		my $serror="";
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error).",previouspage=";
		redirect_header(path => $link);
		exit;
	}
	my $decoded=$rdb->decode($request);
	my $user=$decoded->{user};
	my $portal=$decoded->{object};

	if($option_b eq 'approve') {
		$::PARAM{portal}=string_to_hex($portal);
		$::PARAM{user}=string_to_hex($user);
		$::PARAM{allow}=$comments;
		load_macro('mm_new_grant_portal_access_process');
		return mm_new_grant_portal_access_process();
	}
	if($option_b eq 'reject') {
		$::PARAM{portal}=string_to_hex($portal);
		$::PARAM{user}=string_to_hex($user);
		$::PARAM{denial}=$comments;
		load_macro('mm_deny_portal_access_process');
		return mm_deny_portal_access_process();
	}

	my $r=$rdb->comment_portal_access_request({
		id => $id,
		text => $comments,
		portal => $portal,
		frontend => $::GRI_FRONTEND,
		who_by => $user_name,
	});
	if($r->{error}) {
		my $serror=$r->{error};
		my $error="Failed to update portal access request details for request ID '$id'.";
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error).",previouspage=";
		redirect_header(path => $link);
		exit;
	}

	load_macro('mm_start_page');
	load_macro('mm_header');
	mm_start_page();
	my $s=mm_header(undef,undef,1);
	# Show any info if we have any ... otherwise jump back to outstanding approvals
	if($r->{info}) {
		# Start the real page

		# Add the comment line and a link to the approvals page...

		$s.="<P>Please note the following:<br>$r->{info}<P>";
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/outstanding_approvals_summary";
		$s.="Click <a href=\"$link\">here</a> to return to requests window.";
		return $s;
	}
	my $link="$url_prefix$::ENV{SCRIPT_NAME}/outstanding_approvals_summary";
	$s.="Click <a href=\"$link\">here</a> to return to requests window.";
	return $s;
}

1;
